WWL.calendar = function ( name, attrs )
{
	var cal = new WWL ( 'calendar', name );

	if ( ! attrs ) attrs = {};

	cal._calendar = new OS3Calendar ();
	cal._calendar.is_popup = true;
	cal._hour = "0";
	cal._min  = "0";

	cal.set_attr = function ( name, value )
	{
		cal._calendar [ name ] = value;
	};

	cal.set_event = function ( evt_name, cback )
	{
		cal._calendar.cbacks [ evt_name ] = cback;
	};

	cal.set_date = function ( year, month, day, hour, minutes, quiet ) 
	{
		var undef;

		cal._calendar.set_date ( year, month, day, quiet ); 

		if ( ( hour !== undef ) || ( hour !== "" ) )
		{
			cal._hour = hour;
			cal._min  = minutes;

			if ( cal._calendar._rendered && ! quiet )
			{
				cal.form.set_value ( "hour", hour );
				cal.form.set_value ( "min", minutes );
			}

			cal.set_time ( hour, minutes );
		}

		if ( cal._calendar._rendered && ! quiet )
		{
			cal.form.set_value ( "gg", day );
			cal.form.set_value ( "mm", month );
			cal.form.set_value ( "aaaa", year );
		}
	};

	cal.get_date = function () { return cal._calendar.cur_date; };

	cal.form = new liwe.form.instance ( cal.id );
	cal.form.number ( { name: "gg", size: 2, nonl: true, onblur: "WWL.calendar._update_date('" + name + "')" } );
	cal.form.number ( { name: "mm", size: 2, nonl: true, onblur: "WWL.calendar._update_date('" + name + "')" } );
	cal.form.number ( { name: "aaaa", size: 4, nonl: true, onblur: "WWL.calendar._update_date('" + name + "')" } );
	cal.form.button ( { value: "&nbsp;", nonl: true, "class": "os3calendar_button", onclick: "WWL.calendar.popup(this,'" + name + "')" } );

	if ( attrs.get ( "time", 0 ) )
	{
		var d = new Date ();
		cal._hour = d.getHours ();
		cal._min  = d.getMinutes ();

		// hack to create a new line
		cal.form._newline ( {} );
		cal.form.number ( { label: "Ora", name: "hour", size: 2, nonl: true, value: cal._hour } );
		cal.form.number ( { name: "min", size: 2, value: cal._min } );
		
		cal._support_time = true;
	}

	cal.form.workspace ( { name: "cal:" + cal.id } );

	cal.to_string = function ()
	{
		return cal.form.get ();
	};

	cal.set_day = function ( dd )
	{
		cal.form.set_value ( "gg", dd );
	};

	cal.set_month = function( mm )
	{
		cal.form.set_value ( "mm", mm );
	};

	cal.set_year = function ( yyyy )
	{
		cal.form.set_value ( "aaaa", yyyy );
	};

	cal.set_time = function ( hh, mm )
	{
		if ( ! cal._support_time ) return;

		if ( hh !== undefined )
		{
			cal._hour = hh;
			cal._min  = mm;
		}

		cal.form.set_value ( "hour", cal._hour );
		cal.form.set_value ( "min", cal._min );
	};

	return cal;
};

WWL.calendar.popup = function ( elem, cal_id )
{
	var cal = WWL.get_instance ( "calendar", cal_id );

	cal._calendar.render ( "cal:" + cal.id );

	if ( cal._calendar.currently_shown )
	{
		cal._calendar.hide ();
	} else {
		cal._calendar.show ( elem );
		cal._calendar.overflow ( elem, "cal:" + cal.id );
	}

};

WWL.calendar._update_date = function ( cal_id )
{
	var cal = WWL.get_instance ( "calendar", cal_id );
	var vals = cal.form.get_values ();

	cal._calendar.set_date ( vals [ 'aaaa' ], vals [ 'mm' ], vals [ 'gg' ] );

	cal.set_time ( cal._hour, cal._min );
};
